package com.yang.studentsapp.ui.friends;

import android.content.ContentValues;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.yang.studentsapp.R;
import com.yang.studentsapp.ViewHolder;
import com.yang.studentsapp.bean.InviteMessage;
import com.yang.studentsapp.bean.InviteMessgeDao;
import com.yang.studentsapp.ui.adapter.CommListenerAdapter;

import java.util.List;

/**
 * Created by ${Ян-Вэнь-Чао} on 2016/10/20.
 */
public class ApplyFragment extends Fragment {
    private ListView mListView;
    private CommListenerAdapter<InviteMessage> mAdapter;
    private InviteMessgeDao messgeDao;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_apply, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        messgeDao = new InviteMessgeDao(getContext());

        mListView = (ListView) getActivity().findViewById(R.id.apply_list);
        mAdapter = new CommListenerAdapter<InviteMessage>(getContext(), null ,R.layout.item_invite_msg) {


            @Override
            public void setViewData(com.yang.studentsapp.ui.adapter.ViewHolder holder, InviteMessage item, int position) {
                String str1 = getResources().getString(R.string.Has_agreed_to_your_friend_request);
                String str2 = getResources().getString(R.string.agree);

                String str3 = getResources().getString(R.string.Request_to_add_you_as_a_friend);
                holder.setText(item.getFrom(), R.id.tv_name)
                        .setText(item.getReason(), R.id.tv_reason)
                        .setVisible(false,R.id.btn_agree);
                if (item.getStatus() == InviteMessage.InviteMesageStatus.BEAGREED) {
                    holder.setText(str1, R.id.tv_reason);
                } else if (item.getStatus() == InviteMessage.InviteMesageStatus.BEINVITEED
                        || item.getStatus() == InviteMessage.InviteMesageStatus.BEAPPLYED
                        || item.getStatus() == InviteMessage.InviteMesageStatus.GROUPINVITATION) {
                    holder.setVisible(true, R.id.btn_agree).setText(str2, R.id.btn_agree);
                    if (item.getReason() == null) {
                        holder.setText(str3, R.id.tv_reason);
                    }
                }
                else if (item.getStatus() == InviteMessage.InviteMesageStatus.AGREED) {

                } else if(item.getStatus() == InviteMessage.InviteMesageStatus.REFUSED){

                } else if(item.getStatus() == InviteMessage.InviteMesageStatus.GROUPINVITATION_ACCEPTED){

                } else if(item.getStatus() == InviteMessage.InviteMesageStatus.GROUPINVITATION_DECLINED){

                }
            }

            @Override
            public void setListener(com.yang.studentsapp.ui.adapter.ViewHolder holder, View convertView) {
                holder.setOnclickListener(R.id.btn_add);
            }



            @Override
            public void onClickBack(View v, int position, ViewHolder holder) {
                InviteMessage mag = getItem(position);
                String s = getResources().getString(R.string.Are_agree_with);
                String s1 = getResources().getString(R.string.Has_agreed_to);
                String s2 = getResources().getString(R.string.Agree_with_failure);
                if (mag != null) {
                    try {
                        if (mag.getStatus() == InviteMessage.InviteMesageStatus.BEINVITEED) {
                            EMClient.getInstance().contactManager().acceptInvitation(mag.getFrom());
                        } else if (mag.getStatus() == InviteMessage.InviteMesageStatus.BEAPPLYED) {
                            EMClient.getInstance().groupManager().acceptApplication(mag.getFrom(), mag.getGroupId());
                        }
                        else if (mag.getStatus() == InviteMessage.InviteMesageStatus.GROUPINVITATION) {
                            EMClient.getInstance().groupManager().acceptInvitation(mag.getGroupId(), mag.getGroupInviter());
                        }
                        holder.setText(s1, R.id.btn_agree);
                        mag.setStatus(InviteMessage.InviteMesageStatus.AGREED);
                        ContentValues values = new ContentValues();
                        values.put(InviteMessgeDao.COLUMN_NAME_STATUS, mag.getStatus().ordinal());
                        messgeDao.updateMessage(mag.getId(), values);
                        FragmentManager manager = getActivity().getSupportFragmentManager();
                        ContactsFragment contactsFragment = (ContactsFragment) manager.findFragmentByTag("ContactsFragment");
                        contactsFragment.upDataContact();
                    } catch (HyphenateException e) {
                        Toast.makeText(getContext(), s2 + e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                }
            }
        };
        mListView.setAdapter(mAdapter);
        steDta();
    }

    private void steDta() {
        List<InviteMessage> msgs = messgeDao.getMessagesList();
        Toast.makeText(getContext(),""+msgs.size(),Toast.LENGTH_SHORT).show();
        mAdapter.update(msgs);
    }
}
